import { ElNotification } from 'element-plus'
import { i18n } from '@/stores/i18n'

/** 密钥 */
const secretKey = "VIgzYRkbqFBP";

/** 通知 */
function notification(obj: {
  title?: string,
  message: string
  type: 'success' | 'warning' | 'info' | 'error',
  position?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left',
  duration?: number
}) {
  ElNotification.closeAll();
  ElNotification({
    title: translate(obj.title || '提示'),
    message: translate(obj.message),
    type: obj.type,
    position: obj.position || 'top-right',
    duration: obj.duration
  })
}

/** 翻译 */
function translate(content: string) {
  return i18n.global.t(content);
}

/** 参数转换（object转form-data） */
function objectToFormData(obj: { [key: string]: string }) {
  const fd = new FormData();
  Object.keys(obj).forEach((item: any) => {
    if (obj[item] === undefined || obj[item] === null) {
      fd.append(item, '');
    } else if (typeof obj[item] === 'object') {
      fd.append(item, JSON.stringify(obj[item]));
    } else {
      fd.append(item, String(obj[item]));
    }
  });
  return fd;
}

export {
  notification,
  objectToFormData,
  secretKey
}